from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
                           QLabel, QListWidget, QListWidgetItem, QMenu)
from PyQt6.QtCore import Qt, pyqtSignal
import time

class BookmarkListDialog(QDialog):
    bookmarkSelected = pyqtSignal(dict)  # 发送选中的书签数据
    bookmarkDeleted = pyqtSignal(str)    # 发送要删除的书签ID
    
    def __init__(self, bookmarks, parent=None):
        super().__init__(parent)
        self.bookmarks = bookmarks
        self.setWindowTitle("书签列表")
        self.setMinimumSize(400, 500)
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout(self)
        
        # 创建列表
        self.list_widget = QListWidget()
        self.list_widget.setStyleSheet("""
            QListWidget {
                background: #2B2B2B;
                border: 1px solid #4CAF50;  /* 绿色边框 */
                border-radius: 4px;
                padding: 4px;
            }
            QListWidget::item {
                padding: 12px;  /* 增加内边距 */
                border-bottom: 1px solid #3F3F3F;
                margin-bottom: 4px;  /* 增加项目间距 */
                border-radius: 4px;  /* 圆角 */
            }
            QListWidget::item:selected {
                background: rgba(76, 175, 80, 0.3);  /* 绿色选中背景 */
                border: 1px solid #4CAF50;
            }
            QListWidget::item:hover {
                background: rgba(76, 175, 80, 0.2);  /* 绿色悬停背景 */
            }
        """)
        
        # 添加书签项
        for bookmark in sorted(self.bookmarks, 
                             key=lambda x: x['create_time'], 
                             reverse=True):
            self.add_bookmark_item(bookmark)
        
        # 连接信号
        self.list_widget.itemClicked.connect(self.on_item_clicked)
        self.list_widget.itemDoubleClicked.connect(self.on_item_double_clicked)
        self.list_widget.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
        self.list_widget.customContextMenuRequested.connect(self.show_context_menu)
        
        layout.addWidget(self.list_widget)
        
        # 添加按钮
        btn_layout = QHBoxLayout()
        close_btn = QPushButton("关闭")
        close_btn.setStyleSheet("""
            QPushButton {
                background: #4CAF50;
                border: none;
                border-radius: 4px;
                color: white;
                padding: 8px 16px;
                font-weight: bold;
            }
            QPushButton:hover {
                background: #45a049;
            }
            QPushButton:pressed {
                background: #3d8b40;
            }
        """)
        close_btn.clicked.connect(self.close)
        btn_layout.addStretch()
        btn_layout.addWidget(close_btn)
        
        layout.addLayout(btn_layout)
        
    def add_bookmark_item(self, bookmark):
        """添加书签项"""
        item = QListWidgetItem()
        item.setData(Qt.ItemDataRole.UserRole, bookmark)
        
        # 创建显示的文本
        display_text = (
            f"{bookmark['name']}\n"
            f"章节: {bookmark['chapter_index'] + 1}\n"
            f"创建时间: {bookmark['create_time']}"
        )
        item.setText(display_text)
        
        self.list_widget.addItem(item)
        
    def on_item_clicked(self, item):
        """单击书签项"""
        bookmark = item.data(Qt.ItemDataRole.UserRole)
        self.bookmarkSelected.emit(bookmark)
        
    def on_item_double_clicked(self, item):
        """双击书签项"""
        bookmark = item.data(Qt.ItemDataRole.UserRole)
        self.bookmarkSelected.emit(bookmark)
        self.close()
        
    def show_context_menu(self, pos):
        """显示右键菜单"""
        item = self.list_widget.itemAt(pos)
        if item:
            menu = QMenu(self)
            delete_action = menu.addAction("删除书签")
            action = menu.exec(self.list_widget.mapToGlobal(pos))
            
            if action == delete_action:
                bookmark = item.data(Qt.ItemDataRole.UserRole)
                self.bookmarkDeleted.emit(bookmark['id'])
                self.list_widget.takeItem(self.list_widget.row(item)) 